/*
 * @lc app=leetcode.cn id=733 lang=javascript
 *
 * [733] 图像渲染
 */

// @lc code=start
/**
 * @param {number[][]} image
 * @param {number} sr
 * @param {number} sc
 * @param {number} newColor
 * @return {number[][]}
 */
var floodFill = function (image, sr, sc, newColor) {
    if (image[sr][sc] == newColor) return image
    let old = image[sr][sc]
    let dfs = function (sr, sc) {
        image[sr][sc] = newColor
        if (sr-1>=0&&image[sr - 1][sc] == old) { dfs(sr - 1, sc) }
        if (sr+1<image.length&&image[sr + 1][sc] == old) { dfs(sr + 1, sc) }
        if (sc-1>=0&&image[sr][sc - 1] == old) { dfs(sr, sc - 1) }
        if (image[sr][sc + 1] == old) { dfs(sr, sc + 1) }
    }
    dfs(sr, sc)
    return image
};
// @lc code=end

floodFill ([[1,1,1],[1,1,0],[1,0,1]],1,1,2)